home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / fortran / f2c-9510.000 / f2c-9510 / f2c-951007-libs-1.1 / src / put.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-07  |  9.7 KB  |  441 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1993, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. /*
  25.  * INTERMEDIATE CODE GENERATION PROCEDURES COMMON TO BOTH
  26.  * JOHNSON (PORTABLE) AND RITCHIE FAMILIES OF SECOND PASSES
  27. */
  28.  
  29. #include "defs.h"
  30. #include "names.h"        /* For LOCAL_CONST_NAME */
  31. #include "pccdefs.h"
  32. #include "p1defs.h"
  33.  
  34. /* Definitions for   putconst()   */
  35.  
  36. #define LIT_CHAR 1
  37. #define LIT_FLOAT 2
  38. #define LIT_INT 3
  39.  
  40.  
  41. /*
  42. char *ops [ ] =
  43.     {
  44.     "??", "+", "-", "*", "/", "**", "-",
  45.     "OR", "AND", "EQV", "NEQV", "NOT",
  46.     "CONCAT",
  47.     "<", "==", ">", "<=", "!=", ">=",
  48.     " of ", " ofC ", " = ", " += ", " *= ", " CONV ", " << ", " % ",
  49.     " , ", " ? ", " : "
  50.     " abs ", " min ", " max ", " addr ", " indirect ",
  51.     " bitor ", " bitand ", " bitxor ", " bitnot ", " >> ",
  52.     };
  53. */
  54.  
  55. /* Each of these values is defined in   pccdefs   */
  56.  
  57. int ops2 [ ] =
  58. {
  59.     P2BAD, P2PLUS, P2MINUS, P2STAR, P2SLASH, P2BAD, P2NEG,
  60.     P2OROR, P2ANDAND, P2EQ, P2NE, P2NOT,
  61.     P2BAD,
  62.     P2LT, P2EQ, P2GT, P2LE, P2NE, P2GE,
  63.     P2CALL, P2CALL, P2ASSIGN, P2PLUSEQ, P2STAREQ, P2CONV, P2LSHIFT, P2MOD,
  64.     P2COMOP, P2QUEST, P2COLON,
  65.     1, P2BAD, P2BAD, P2BAD, P2BAD,
  66.     P2BITOR, P2BITAND, P2BITXOR, P2BITNOT, P2RSHIFT,
  67.     P2BAD, P2BAD, P2BAD, P2BAD, P2BAD, P2BAD, P2BAD, P2BAD, P2BAD,
  68.     P2BAD, P2BAD, P2BAD, P2BAD,
  69.     1,1,1,1,1, /* OPNEG1, OPDMIN, OPDMAX, OPASSIGNI, OPIDENTITY */
  70.     1,1,1,1    /* OPCHARCAST, OPDABS, OPMIN2, OPMAX2 */
  71. };
  72.  
  73.  
  74.  void
  75. #ifdef KR_headers
  76. putexpr(p)
  77.     expptr p;
  78. #else
  79. putexpr(expptr p)
  80. #endif
  81. {
  82. /* Write the expression to the p1 file */
  83.  
  84.     p = (expptr) putx (fixtype (p));
  85.     p1_expr (p);
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  expptr
  93. #ifdef KR_headers
  94. putassign(lp, rp)
  95.     expptr lp;
  96.     expptr rp;
  97. #else
  98. putassign(expptr lp, expptr rp)
  99. #endif
  100. {
  101.     return putx(fixexpr((Exprp)mkexpr(OPASSIGN, lp, rp)));
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  void
  108. #ifdef KR_headers
  109. puteq(lp, rp)
  110.     expptr lp;
  111.     expptr rp;
  112. #else
  113. puteq(expptr lp, expptr rp)
  114. #endif
  115. {
  116.     putexpr(mkexpr(OPASSIGN, lp, rp) );
  117. }
  118.  
  119.  
  120.  
  121.  
  122. /* put code for  a *= b */
  123.  
  124.  expptr
  125. #ifdef KR_headers
  126. putsteq(a, b)
  127.     Addrp a;
  128.     Addrp b;
  129. #else
  130. putsteq(Addrp a, Addrp b)
  131. #endif
  132. {
  133.     return putx( fixexpr((Exprp)
  134.         mkexpr(OPSTAREQ, cpexpr((expptr)a), cpexpr((expptr)b))));
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  Addrp
  141. #ifdef KR_headers
  142. mkfield(res, f, ty)
  143.     register Addrp res;
  144.     char *f;
  145.     int ty;
  146. #else
  147. mkfield(register Addrp res, char *f, int ty)
  148. #endif
  149. {
  150.     res -> vtype = ty;
  151.     res -> Field = f;
  152.     return res;
  153. } /* mkfield */
  154.  
  155.  
  156.  Addrp
  157. #ifdef KR_headers
  158. realpart(p)
  159.     register Addrp p;
  160. #else
  161. realpart(register Addrp p)
  162. #endif
  163. {
  164.     register Addrp q;
  165.  
  166.     if (p->tag == TADDR
  167.      && p->uname_tag == UNAM_CONST
  168.      && ISCOMPLEX (p->vtype))
  169.         return (Addrp)mkrealcon (p -> vtype + TYREAL - TYCOMPLEX,
  170.             p->user.kludge.vstg1 ? p->user.Const.cds[0]
  171.                 : cds(dtos(p->user.Const.cd[0]),CNULL));
  172.  
  173.     q = (Addrp) cpexpr((expptr) p);
  174.     if( ISCOMPLEX(p->vtype) )
  175.         q = mkfield (q, "r", p -> vtype + TYREAL - TYCOMPLEX);
  176.  
  177.     return(q);
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  expptr
  184. #ifdef KR_headers
  185. imagpart(p)
  186.     register Addrp p;
  187. #else
  188. imagpart(register Addrp p)
  189. #endif
  190. {
  191.     register Addrp q;
  192.  
  193.     if( ISCOMPLEX(p->vtype) )
  194.     {
  195.         if (p->tag == TADDR && p->uname_tag == UNAM_CONST)
  196.             return mkrealcon (p -> vtype + TYREAL - TYCOMPLEX,
  197.                 p->user.kludge.vstg1 ? p->user.Const.cds[1]
  198.                 : cds(dtos(p->user.Const.cd[1]),CNULL));
  199.         q = (Addrp) cpexpr((expptr) p);
  200.         q = mkfield (q, "i", p -> vtype + TYREAL - TYCOMPLEX);
  201.         return( (expptr) q );
  202.     }
  203.     else
  204.  
  205. /* Cast an integer type onto a Double Real type */
  206.  
  207.         return( mkrealcon( ISINT(p->vtype) ? TYDREAL : p->vtype , "0"));
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214. /* ncat -- computes the number of adjacent concatenation operations */
  215.  
  216.  int
  217. #ifdef KR_headers
  218. ncat(p)
  219.     register expptr p;
  220. #else
  221. ncat(register expptr p)
  222. #endif
  223. {
  224.     if(p->tag==TEXPR && p->exprblock.opcode==OPCONCAT)
  225.         return( ncat(p->exprblock.leftp) + ncat(p->exprblock.rightp) );
  226.     else    return(1);
  227. }
  228.  
  229.  
  230.  
  231.  
  232. /* lencat -- returns the length of the concatenated string.  Each
  233.    substring must have a static (i.e. compile-time) fixed length */
  234.  
  235.  ftnint
  236. #ifdef KR_headers
  237. lencat(p)
  238.     register expptr p;
  239. #else
  240. lencat(register expptr p)
  241. #endif
  242. {
  243.     if(p->tag==TEXPR && p->exprblock.opcode==OPCONCAT)
  244.         return( lencat(p->exprblock.leftp) + lencat(p->exprblock.rightp) );
  245.     else if( p->headblock.vleng!=NULL && ISICON(p->headblock.vleng) )
  246.         return(p->headblock.vleng->constblock.Const.ci);
  247.     else if(p->tag==TADDR && p->addrblock.varleng!=0)
  248.         return(p->addrblock.varleng);
  249.     else
  250.     {
  251.         err("impossible element in concatenation");
  252.         return(0);
  253.     }
  254. }
  255.  
  256. /* putconst -- Creates a new Addrp value which maps onto the input
  257.    constant value.  The Addrp doesn't retain the value of the constant,
  258.    instead that value is copied into a table of constants (called
  259.    litpool,   for pool of literal values).  The only way to retrieve the
  260.    actual value of the constant is to look at the   memno   field of the
  261.    Addrp result.  You know that the associated literal is the one referred
  262.    to by   q   when   (q -> memno == litp -> litnum).
  263. */
  264.  
  265.  Addrp
  266. #ifdef KR_headers
  267. putconst(p)
  268.     register Constp p;
  269. #else
  270. putconst(register Constp p)
  271. #endif
  272. {
  273.     register Addrp q;
  274.     struct Literal *litp, *lastlit;
  275.     int k, len, type;
  276.     int litflavor;
  277.     double cd[2];
  278.     ftnint nblanks;
  279.     char *strp;
  280.     char cdsbuf0[64], cdsbuf1[64], *ds[2];
  281.  
  282.     if (p->tag != TCONST)
  283.         badtag("putconst", p->tag);
  284.  
  285.     q = ALLOC(Addrblock);
  286.     q->tag = TADDR;
  287.     type = p->vtype;
  288.     q->vtype = ( type==TYADDR ? tyint : type );
  289.     q->vleng = (expptr) cpexpr(p->vleng);
  290.     q->vstg = STGCONST;
  291.  
  292. /* Create the new label for the constant.  This is wasteful of labels
  293.    because when the constant value already exists in the literal pool,
  294.    this label gets thrown away and is never reclaimed.  It might be
  295.    cleaner to move this down past the first   switch()   statement below */
  296.  
  297.     q->memno = newlabel();
  298.     q->memoffset = ICON(0);
  299.     q -> uname_tag = UNAM_CONST;
  300.  
  301. /* Copy the constant info into the Addrblock; do this by copying the
  302.    largest storage elts */
  303.  
  304.     q -> user.Const = p -> Const;
  305.     q->user.kludge.vstg1 = p->vstg;    /* distinguish string from binary fp */
  306.  
  307.     /* check for value in literal pool, and update pool if necessary */
  308.  
  309.     k = 1;
  310.     switch(type)
  311.     {
  312.     case TYCHAR:
  313.         if (halign) {
  314.             strp = p->Const.ccp;
  315.             nblanks = p->Const.ccp1.blanks;
  316.             len = p->vleng->constblock.Const.ci;
  317.             litflavor = LIT_CHAR;
  318.             goto loop;
  319.             }
  320.         else
  321.             q->memno = BAD_MEMNO;
  322.         break;
  323.     case TYCOMPLEX:
  324.     case TYDCOMPLEX:
  325.         k = 2;
  326.         if (p->vstg)
  327.             cd[1] = atof(ds[1] = p->Const.cds[1]);
  328.         else
  329.             ds[1] = cds(dtos(cd[1] = p->Const.cd[1]), cdsbuf1);
  330.     case TYREAL:
  331.     case TYDREAL:
  332.         litflavor = LIT_FLOAT;
  333.         if (p->vstg)
  334.             cd[0] = atof(ds[0] = p->Const.cds[0]);
  335.         else
  336.             ds[0] = cds(dtos(cd[0] = p->Const.cd[0]), cdsbuf0);
  337.         goto loop;
  338.  
  339.     case TYLOGICAL1:
  340.     case TYLOGICAL2:
  341.     case TYLOGICAL:
  342.     case TYLONG:
  343.     case TYSHORT:
  344.     case TYINT1:
  345. #ifdef TYQUAD
  346.     case TYQUAD:
  347. #endif
  348.  lit_int_flavor:
  349.         litflavor = LIT_INT;
  350.  
  351. /* Scan the literal pool for this constant value.  If this same constant
  352.    has been assigned before, use the same label.  Note that this routine
  353.    does NOT consider two differently-typed constants with the same bit
  354.    pattern to be the same constant */
  355.  
  356.  loop:
  357.         lastlit = litpool + nliterals;
  358.         for(litp = litpool ; litp<lastlit ; ++litp)
  359.  
  360. /* Remove this type checking to ensure that all bit patterns are reused */
  361.  
  362.             if(type == litp->littype) switch(litflavor)
  363.             {
  364.             case LIT_CHAR:
  365.                 if (len == (int)litp->litval.litival2[0]
  366.                 && nblanks == litp->litval.litival2[1]
  367.                 && !memcmp(strp, litp->cds[0], len)) {
  368.                     q->memno = litp->litnum;
  369.                     frexpr((expptr)p);
  370.                     q->user.Const.ccp1.ccp0 = litp->cds[0];
  371.                     return(q);
  372.                     }
  373.                 break;
  374.             case LIT_FLOAT:
  375.                 if(cd[0] == litp->litval.litdval[0]
  376.                 && !strcmp(ds[0], litp->cds[0])
  377.                 && (k == 1 ||
  378.                     cd[1] == litp->litval.litdval[1]
  379.                     && !strcmp(ds[1], litp->cds[1]))) {
  380. ret:
  381.                     q->memno = litp->litnum;
  382.                     frexpr((expptr)p);
  383.                     return(q);
  384.                     }
  385.                 break;
  386.  
  387.             case LIT_INT:
  388.                 if(p->Const.ci == litp->litval.litival)
  389.                     goto ret;
  390.                 break;
  391.             }
  392.  
  393. /* If there's room in the literal pool, add this new value to the pool */
  394.  
  395.         if(nliterals < maxliterals)
  396.         {
  397.             ++nliterals;
  398.  
  399.             /* litp   now points to the next free elt */
  400.  
  401.             litp->littype = type;
  402.             litp->litnum = q->memno;
  403.             switch(litflavor)
  404.             {
  405.             case LIT_CHAR:
  406.                 litp->litval.litival2[0] = len;
  407.                 litp->litval.litival2[1] = nblanks;
  408.                 q->user.Const.ccp = litp->cds[0] =
  409.                     memcpy(gmem(len,0), strp, len);
  410.                 break;
  411.  
  412.             case LIT_FLOAT:
  413.                 litp->litval.litdval[0] = cd[0];
  414.                 litp->cds[0] = copys(ds[0]);
  415.                 if (k == 2) {
  416.                     litp->litval.litdval[1] = cd[1];
  417.                     litp->cds[1] = copys(ds[1]);
  418.                     }
  419.                 break;
  420.  
  421.             case LIT_INT:
  422.                 litp->litval.litival = p->Const.ci;
  423.                 break;
  424.             } /* switch (litflavor) */
  425.         }
  426.         else
  427.             many("literal constants", 'L', maxliterals);
  428.  
  429.         break;
  430.     case TYADDR:
  431.         break;
  432.     default:
  433.         badtype ("putconst", p -> vtype);
  434.         break;
  435.     } /* switch */
  436.  
  437.     if (type != TYCHAR || halign)
  438.         frexpr((expptr)p);
  439.     return( q );
  440. }
  441.